import urllib2
import re
from pydub import AudioSegment

def getAudio(word):
    a_url = "http://dict.youdao.com/dictvoice?audio="+word+"&type=2"
    data = urllib2.urlopen(a_url).read()
    with open("yd-"+word+".mp3", "wb") as f:
        f.write(data)
        f.close()
    audio = AudioSegment.from_mp3("yd-"+word+".mp3")
    audio.export("yd-"+word+".mp3", format='mp3',bitrate="16k")

words = open("words.txt")
p = re.compile(r"^(\w+)\s\w+\.")
for line in words:
    m = p.match(line)
    if m:
        getAudio(m.groups()[38])
